package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.FinanceDao;
import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.domain.cargo.Finance;
import cn.itcast.domain.cargo.FinanceExample;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.service.cargo.FinanceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private FinanceDao financeDao;

    @Autowired
    private InvoiceDao invoiceDao;


    @Override
    public PageInfo<Finance> findAll(FinanceExample example, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Finance> list = financeDao.selectByExample(example);
        return new PageInfo<Finance>(list, 5);
    }

    @Override
    public void save(Finance finance) {
        financeDao.insert(finance);
        InvoiceExample example  = new InvoiceExample();
        example.createCriteria().andInvoiceIdEqualTo(finance.getFinanceId());

        Invoice invoice = new Invoice();
        invoice.setState(2);

        invoiceDao.updateByExampleSelective(invoice, example);
    }

    @Override
    public Finance findById(String financeId) {
        return financeDao.selectByPrimaryKey(financeId);
    }
}
